package com.pine.app.module.security.core.authorize.metadataSource;

import com.pine.app.module.security.core.matcher.RequestMatcher;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author xiaoyuan
 * @create 2020/3/23 22:09
 **/
public class FilterInvocationSecurityMetadataSource implements SecurityMetadataSource  {

    private Map<RequestMatcher, List<ConfigAttribute>> requestMap = null ;

    private SecurityMetadataSource securityMetadataSource;

    @Autowired
    public void setSecurityMetadataSource(SecurityMetadataSource securityMetadataSource) {
        this.securityMetadataSource = securityMetadataSource;
        setConfig(getConfig());
    }

    public FilterInvocationSecurityMetadataSource() {

    }

    public FilterInvocationSecurityMetadataSource(SecurityMetadataSource securityMetadataSource) {
       this.securityMetadataSource = securityMetadataSource;

    }
    @Override
    public Map<RequestMatcher, List<ConfigAttribute>> getConfig() {
        return securityMetadataSource.getConfig();
    }

    @Override
    public List<ConfigAttribute> getConfigAttribute(HttpServletRequest request) {
        return getAttributes(request);
    }

    @Override
    public void setConfig(Map<RequestMatcher, List<ConfigAttribute>> matcherListMap) {
        if(this.requestMap==null)
            this.requestMap = matcherListMap;
    }

    public List<ConfigAttribute> getAttributes(HttpServletRequest request) {
        Iterator var3 = this.requestMap.entrySet().iterator();
        Map.Entry entry;
        do {
            if(!var3.hasNext()) {
                return null;
            }
            entry = (Map.Entry)var3.next();
        } while(!((RequestMatcher)entry.getKey()).matches(request));
        return (List<ConfigAttribute>) entry.getValue();
    }
}
